package com.yh.tea.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yh.tea.entity.Menu;
import com.yh.tea.entity.Resource;
import com.yh.tea.mapper.ResourceMapper;
import com.yh.tea.model.ResourceCascader;
import com.yh.tea.service.ResourceService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yh.tea.utiity.ResponseResult;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yh
 * @since 2024-03-04
 */
@Service
public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> implements ResourceService {

   // public QueryWrapper<Resource> queryWrapper=new QueryWrapper<>();
    @Override
    public ResponseResult getMenu(List resourceIdList) {

        List<Resource> resourceList=new ArrayList<>();
        for (Object o : resourceIdList) {
            resourceList.add(getOne(new QueryWrapper<Resource>().eq("id",o).eq("resource_type","菜单")));
        }
        List<Menu> menuList= findMenu(0L,resourceList);
        return ResponseResult.success("获取成功",menuList);
    }
    public List<Menu> findMenu(Long parentId, List<Resource> resourceList){
        List<Menu> menuList=new ArrayList<>();
        for (Resource o : resourceList) {
            if(o.getParentId()==parentId){
                Menu menu=new Menu();
                menu.setId(o.getId());
                menu.setResourceName(o.getResourceName());
                menu.setIcon(o.getIcon());
                menu.setRouterName(o.getRouterName());
                menuList.add(menu);
            }
        }
        for (Menu menu : menuList) {
            menu.setChildMenu(findMenu(menu.getId(),resourceList));
        }
        return menuList;
    }
    @Override
    public ResponseResult getResourceCascader(){
        List<Resource> resourceList= list();
        List<Resource> newResourceList=findResource(0L,resourceList);
        List<ResourceCascader> list=findResourceCascader(newResourceList);
        return ResponseResult.success("查询成功",list);
    }
    @Override
    public ResponseResult getResource(){
        List<Resource> resourceList= list();
        //List<Resource> newResourceList=findResource(0L,resourceList);
        return ResponseResult.success("查询成功",resourceList);
    }
    public List<Resource> findResource(Long parentId,List<Resource> resourceList){
        List<Resource> list=new ArrayList<>();
        for (Resource o : resourceList) {
            if(o.getParentId()==parentId){
                Resource resource=new Resource(o.getId(),o.getParentId(),o.getResourceName(),o.getResourceType(),o.getIcon(),o.getRouterName(),o.getUrl(),o.getResourceList());
                list.add(resource);
            }
        }
        for (Resource resource : list) {
            resource.setResourceList(findResource(resource.getId(),resourceList));
        }

        return list;
    }
    public List<ResourceCascader> findResourceCascader(List<Resource> resourceList){
        List<ResourceCascader> resourceCascaderList =new ArrayList<>();
         for (Resource resource : resourceList) {
             ResourceCascader resourceCascader=new ResourceCascader();
             if(resource.getResourceList()!=null){
                resourceCascader.setResourceList(findResourceCascader(resource.getResourceList())) ;
             }
             resourceCascader.setResourceName(resource.getResourceName());
             resourceCascader.setId(resource.getId());
             resourceCascaderList.add(resourceCascader);
         }
         return resourceCascaderList;
     }
}
